//
//  NWDiscoverSearchView.swift
//  Weibo
//
//  Created by Northwan on 16/1/20.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWDiscoverSearchView: UIView , UITextFieldDelegate{
    
    @IBOutlet weak var cancelButton: UIButton!
    
    @IBOutlet weak var contentTextField: UITextField!
    
    @IBOutlet weak var contentTextFieldRightConstant: NSLayoutConstraint!
    
    class func searchView() -> NWDiscoverSearchView{
        return NSBundle.mainBundle().loadNibNamed("NWDiscoverSearchView", owner: nil, options: nil).last! as! NWDiscoverSearchView
    }

    override func awakeFromNib() {
        //设置leftView
        let leftView = UIImageView(image: UIImage(named:"searchbar_textfield_search_icon"))
        leftView.frame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
        leftView.contentMode = .Center
        contentTextField.leftView = leftView
        contentTextField.leftViewMode = .Always
    }
    
    // MARK: - 点击事件
    @IBAction func cancelButtonClick(sender: UIButton) {
        //取消第一响应者
        self.contentTextField.resignFirstResponder()
        
        contentTextFieldRightConstant.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            //对重新布局的执行动画
            self.contentTextField.layoutIfNeeded()
        }
    }

    // MARK: - UITextFieldDelegate
    func textFieldDidBeginEditing(textField: UITextField) {
        contentTextFieldRightConstant.constant = cancelButton.frame.width
        UIView.animateWithDuration(0.25) { () -> Void in
            //对重新布局的执行动画
            self.contentTextField.layoutIfNeeded()
        }
    }
}
